var schedule_cur={
	constant:{
		pageNo:1,
		pageSize:15,
	}
}
var tale = new $.tale();//弹框
;$(function() {
	var id = $("#id").val();
	getDeptList();
	save_schedule();
})

//分页查询
function geScheduleList(){
	var supervisionId = $("#id").val();
		$.ajax({
	    	url: ctxPath+"/supervise/schedule/getPage",
	        type: 'POST',
	        data: {'supervisionId':supervisionId,pageNo:schedule_cur.constant.pageNo,pageSize:schedule_cur.constant.pageSize},
	        success: function(d){
	        	 if(d.data!=null){
	        		 var html="";
	        	$.each(d.data.dataList,function(i,item){
	        		html+=toScheduleHtml(item);
	        	})	
	        	$("#schedule_result").html(html);
	        	} 
	        getSchedulePage(d.data.pages,d.data.total);	 
	        }
	    });
	}

 function save_schedule(){
	 $("#schedule_form").validator({
	        rules: {
	            check_schedule:function(element){
		    		var name = element.name;
		    		var json={};
		    		   json["schedule"] = element.value;
		    		  var value = $("#id").val();
		    		  json["supervisionId"] = value;
		    		  var schedule_value = $("#schedule_id").val();
		    		  if(schedule_value!=null&&schedule_value!=''){
		    			  var id="id";
		    			  json[id] = schedule_value;
		    		  }
		    		var isValid = true;
		        	$.ajax({
		        		 url: ctxPath+'/supervise/schedule/checkSchedule',
		                 type: 'post',
		                 data: json,
		                 dataType: 'json',
		                 async:false,
		                 success: function(d){
		                	 isValid =  d;
		                 }
		        	})
		        	return isValid|| "进度超过100%了";
		    	}
	        },
	        fields: {
	        	schedule: {
	                rule: "required;range(1~100);check_schedule",
	                msg: {
	                    required: "必填!"
	                }
	            },
	            time: {
	                rule: "required;checked",
	                msg: {
	                    required: "必填!"
	                }
	            },
	            'userId': {
	                rule: "required;",
	                msg: {
	                    required: "必填!"
	                }
	            },
	            'content': {
	            	rule: "required;",
	            	msg: {
	            		required: "必填!"
	            	}
	            },
	        },
	        invalid: function (form, errors) {
	            console.log(errors);
	            $(".btn-submit").removeAttr("style");
	        },
	        //验证成功
	        valid: function (form) {
	        	saveScheduleInfo();
	        },
	    });
 }

 function saveScheduleInfo() {
	    var json = $("#schedule_form").serializeFormJSON();
	    console.log(json);
	    var id = $("#schedule_id").val();
	    var url = ctxPath+"/supervise/schedule/saveInfo";
	    if (id != '' && id != null) {
	        url = ctxPath+"/supervise/schedule/updateInfo";
	    }
	    $.ajax({
	        url: url,
	        type: 'POST',
	        data: json,
	        success: function (d) {
	            var success = d.success;
	            if (success) {
	                tale.alertOk({
	                    text: '保存成功',
	                    then: function () {
	                    	$("#scheduleModal").modal('hide');
	                        setTimeout(function () {
	                        	geScheduleList();
	                        }, 500);
	                    }
	                });
	            } else {
	                tale.alertError(d.message || '保存失败');
	            }
	        }
	    });
	}
 
	//分页
	function getSchedulePage(pageCount,totalCount){
		kkpager.total =pageCount ;//总页码
		kkpager.totalRecords = totalCount;//总条数
		kkpager.generPageHtml({
			pno : 0,
			mode : 'click',//默认值是link，可选link或者click
			click : function(n){
				schedule_cur.constant.pageNo = n;
				geScheduleList();
				this.selectPage(n);
			    return false;
			}
		});
	}
	
	
	function addSchedule(){
		var supervisionId = $("#id").val();
		$("#scheduleModal").modal('show');
	}
	
	
	
	/**
	 * 获取组织机构
	 * @returns
	 */
	function getDeptList(){
		$.ajax({
	        url: ctxPath+"/agency/dept/getAllList",
	        type: 'GET',
	        success: function(d){
	        	var success = d.success;
	        	 if(success==true){
	        	if(d.data!=null){
	        		var result ='<option value="">请选择</option>';
	        		var deptId="";
	        	$.each(d.data,function(i,item){
	        		result+='<option value="'+item.id+'">'+item.name+'</option>';	
	        	})
	        	$("#dept_select").html(result);
//	        	getUserList(deptId);
	        	}	 
	        	}
	        	 
		}
	   })
	}


	/**
	 * 获取机构下的人员
	 * @returns
	 */
	function getScheduleUserList(deptId,userId){
		var result ='<option value="">请选择</option>';
		if(deptId==null||deptId==''){
			$("#user_select").html(result);
			return;
		}
		$.ajax({
			url: ctxPath+"/agency/user/findByDeptId/"+deptId,
			type: 'GET',
			success: function(d){
				var success = d.success;
				if(success==true){
					if(d.data!=null){
						$.each(d.data,function(i,item){
							result+='<option value="'+item.userId+'">'+item.userName+'</option>';	
						})
						$("#user_select").html(result);
						$("#user_select").val(userId);
						
					}	 
				}
				
			}
		})
	}
	
	
	function toScheduleHtml(data){
		 return '<tr>'
	         +'<td>'
	         +data.nickname
	         +'</td>'
	         +'<td>'
	         +'<div style="white-space: nowrap;"><div class="progress" style="width:80%;">'
	         +'<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="'+data.schedule+'" aria-valuemin="0" aria-valuemax="100" style="width: '+data.schedule+'%;">'
	         +'<span class="sr-only"></span>'
	         +'</div>'
	         +'</div><div>完成'+data.schedule+'%</div>'
	         +'</div></td>'
	         +'<td>'
	         +data.content
	         +'</td>'
	         +'<td>'
	         +data.time
	         +'</td>'
	         +'<td>'
	         +'<button  class="btn btn-primary btn-sm waves-effect waves-light m-b-5" onclick="edit_schedule(' + data.id + ')"><i class="fa fa-edit"></i>编辑</button>'
	         +'<button type="button" class="btn btn-danger btn-sm waves-effect waves-light m-b-5" onclick="del_schedule(' + data.id + ')"><i class="fa fa-trash-o"></i> 删除</button>'
	         +'</td>'
	         +'</tr>';
	 }
	
	function edit_schedule(id){
		$("#schedule_id").val(id);
		var supervisionId = $("#id").val();
		getInfo_schedule(id);
		$("#scheduleModal").modal('show');
	}
	
	
	
	function getInfo_schedule(id) {
	    $.ajax({
	        url: ctxPath+"/supervise/schedule/findById/" + id,
	        type: 'GET',
	        success: function (d) {
	            var success = d.success;
	            if (success == true) {
	                var data = d.data;
	                $("#time").val(data.time);
	                $("#content").val(data.content);
	                $("#schedule_num").val(data.schedule);
	                $("#dept_select").val(data.deptId);
	                getScheduleUserList(data.deptId,data.userId);
	            }
	        },
	        error: function () {
	        }

	    });
	}
	
	 
   function del_schedule(id,name){
	    tale.alertConfirm({
	        title:'确定删除吗?',
	        then: function () {
	            tale.post({
	                url : ctxPath+'/supervise/schedule/del',
	                data: {id: id},
	                success: function (result) {
	                    if(result && result.success == true){
	                    	geScheduleList();
//	                        tale.alertOkAndReload('删除成功');
	                    } else {
	                        tale.alertError(result.msg || '删除失败');
	                    }
	                }
	            });
	        }
	    });
	 }